<?php

namespace app\health\controller;

use app\health\model\HealthUserAccount;
use think\Db;

/**
 * 用户账户记录
 * @Author 张旭鹏
 * @DateTime 2023-06-13
 */
class UserAccount extends ApiBase
{

    /**
     * 获取资金类型列表
     * @Author 张旭鹏
     * @DateTime 2023-06-13
     */
    public function type()
    {
        $data        = HealthUserAccount::MAP_TYPE['account_type'];
        $accountType = [];
        foreach ($data as $type => $name) {
            $accountType[] = ['type' => $type, 'name' => $name];
        }
        $this->success('', $accountType);
    }

    /**
     * 资金记录列表
     * @Author 张旭鹏
     * @DateTime 2023-06-13
     */
    public function lists()
    {
        $limit        = $this->request->param('limit/d', 10);
        $account_type = $this->request->param('account_type', HealthUserAccount::ACCOUNT_TYPE_BALANCE);
        $month        = $this->request->param('month', date('Y-m'));
        $paginate     = HealthUserAccount::where(['user_id' => $this->auth->id, 'account_type' => $account_type])
            ->field('id,change_type,value,desc,created_at')
            ->where('DATE_FORMAT(created_at,"%Y-%m")="' . $month . '"')
            ->order('id', 'desc')
            ->paginate($limit)
            ->each(function ($item) {
                $item->created_at = date('m月d日 H:i', strtotime($item->created_at));
            });
        if ((int)$account_type === HealthUserAccount::ACCOUNT_TYPE_BALANCE) {
            Db::name('health_tracking_monitor')->insert([
                'event_id'    => 12,
                'user_id'     => $this->auth->id ?: 0,
                'ip'          => $this->request->ip(),
                'create_time' => time()
            ]);
        }
        $this->success('', $paginate);
    }
}
